import React from 'react';
// import MDDRenderEngine, { JSONWatch } from '@cniot/mdd-render-engine';
import MDDRenderEngine, { JSONWatch } from '../../mdd-render-engine/MDDRenderEngine';
import schema from './mdd.schema.json';

// eslint-disable-next-line import/no-anonymous-default-export
export default function (props) {
  const { params, closeSelf, parentRefresh, schema: _schema } = props;
  const { isParentRefresh } = _schema || {};
  // 刷新父页面
  const onReady = React.useCallback((engine) => {
    engine.on('form:finish', () => {
      closeSelf && closeSelf();
      if (isParentRefresh) {
        parentRefresh();
      }
    });
  }, []);

  return <MDDRenderEngine schema={schema} modules={createModules(params)} onReady={onReady}></MDDRenderEngine>;
}

/**
 * 如果是弹窗，需要初始化 $PARAMS 模块用于传值
 */
function createModules(params = {}) {
  return React.useState(() => {
    return new Map([['$PARAMS', new JSONWatch(params)]]);
  })[0];
}
